package com.rfy.user.controller;


import com.rfy.base.entity.response.BaseResponse;
import com.rfy.user.api.BookmarkApi;
import com.rfy.user.entity.Bookmark;
import com.rfy.user.service.BookmarkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.rfy.base.constant.BookmarkConstants.*;
import static com.rfy.base.constant.GlobalConstants.FAIL;
import static com.rfy.base.constant.GlobalConstants.SUCCESS;


/**
 * @author simaduoduo
 * @date 2019/7/25 14:49
 * @desc 书签模块
 */
@RestController
public class BookmarkController implements BookmarkApi {
    private static final Logger logger = LoggerFactory.getLogger(BookmarkController.class);

    @Autowired
    private BookmarkService bookmarkService;

    @Override
    @ResponseBody
    public BaseResponse addBookmark(@RequestBody Bookmark bookmark) {
        BaseResponse baseResponse = new BaseResponse();
        int code = bookmarkService.addBookmark(bookmark);
        if (code == -1) {
            baseResponse.setStatus(FAIL);
            baseResponse.setMessage(BOOKMARK_NUMBER_FULL);
        } else {
            baseResponse.setStatus(SUCCESS);
            baseResponse.setMessage(SAVE_BOOKMARK_SUCCESS);
        }
        return baseResponse;
    }

    @Override
    @ResponseBody
    public BaseResponse deleteBookmark(@PathVariable(value = "id") Integer id) {
        BaseResponse baseResponse = new BaseResponse();
        if (bookmarkService.removeBookmark(id) == 0) {
            baseResponse.setStatus(FAIL);
            baseResponse.setMessage(DELETE_BOOKMARK_FAIL);
        } else {
            baseResponse.setStatus(SUCCESS);
            baseResponse.setMessage(DELETE_BOOKMARK_SUCCESS);
        }
        return baseResponse;
    }

    @Override
    @ResponseBody
    public BaseResponse updateBookmark(@RequestBody Bookmark bookmark) {
        BaseResponse baseResponse = new BaseResponse();
        if (bookmarkService.updateBookmark(bookmark) == 0) {
            baseResponse.setStatus(FAIL);
            baseResponse.setMessage(UPDATE_BOOKMARK_FAIL);
        } else {
            baseResponse.setStatus(SUCCESS);
            baseResponse.setMessage(UPDATE_BOOKMARK_SUCCESS);
        }
        return baseResponse;
    }

    @Override
    @ResponseBody
    public BaseResponse queryBookmarks(@PathVariable(value = "userId") Integer userId) {
        BaseResponse baseResponse = new BaseResponse();
        List<Bookmark> result = bookmarkService.queryBookmarks(userId);
        baseResponse.setStatus(SUCCESS);
        baseResponse.setResult(result);
        logger.info(result.toString());
        return baseResponse;
    }
}
